package com.wawei.seeds;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.wawei.seeds.champion.ChampionFragment;
import com.wawei.seeds.champion.ChampionMoreActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class MainActivity extends ActionBarActivity {

    //Current tag fragment, currentFragmentTag will be the tab's tag name while the tab is clicked
    private String currentFragmentTag;

    @InjectView(R.id.bottomBar) RadioGroup bottomBar;
    @InjectView(R.id.tabTitle) TextView tabTitle;
    @InjectView(R.id.tabTitleMore) TextView moreBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        moreBtn.setOnClickListener(moreBtnClickedListener);

        bottomBar.setOnCheckedChangeListener(onCheckedChangeListener);
        bottomBar.check(R.id.championBtn);
        switchTabFragment(ChampionFragment.newInstance(), getResources().getString(R.string.title_champion));
    }

    private TextView.OnClickListener moreBtnClickedListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ChampionMoreActivity.class);
            startActivity(intent);
        }
    };

    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.championBtn:
                    switchTabFragment(ChampionFragment.newInstance(), getResources().getString(R.string.title_champion));
                    break;
                case R.id.heroBtn:
                    break;
                case R.id.resourceBtn:
                    break;
                case R.id.meBtn:
                    break;
                default:
                    break;
            }
        }
    };

    private void switchTabFragment(Fragment fragment, String tag){
        if(null != currentFragmentTag && currentFragmentTag.equals(tag))
            return;
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment currentFragment = fragmentManager.findFragmentByTag(currentFragmentTag);
        if(null != fragmentManager.findFragmentByTag(currentFragmentTag)){
            fragmentManager.beginTransaction().hide(currentFragment).commit();
            tabTitle.setText("");
        }
        if(null != fragmentManager.findFragmentByTag(tag)){
            fragmentManager.beginTransaction().show(fragment).commit();
        }
        else{
            fragmentManager.beginTransaction().add(R.id.fragmentContainer, fragment, tag).commit();
        }
        currentFragmentTag = tag;
        tabTitle.setText(tag);
    }
}
